package com.hzcc.hzccblog.module.controller;

import com.hzcc.hzccblog.common.Result;
import com.hzcc.hzccblog.common.enums.ArticleEnums;
import com.hzcc.hzccblog.common.enums.AuthEnums;
import com.hzcc.hzccblog.common.enums.PublicEnums;
import com.hzcc.hzccblog.module.entity.dto.AddArticle;
import com.hzcc.hzccblog.module.entity.dto.FindByATypeDto;
import com.hzcc.hzccblog.module.entity.dto.RedisPageDto;
import com.hzcc.hzccblog.module.entity.po.ArticleImage;
import com.hzcc.hzccblog.module.service.ArticleImageService;
import com.hzcc.hzccblog.module.service.ArticleService;
import com.hzcc.hzccblog.module.service.AuthService;
import com.hzcc.hzccblog.utils.FileUpdateUtil;
import com.hzcc.hzccblog.utils.IDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 早睡蛋
 * @Date 2022/6/8 10:10:52
 * @Desc:
 */
@RestController
@CrossOrigin
@Slf4j
@Api(tags = "文章相关接口管理")
@RequestMapping("api/article")
public class ArticleController {

    @Value("${articleImages.dir}")
    private String articleImages;

    @Autowired
    private ArticleService articleService;
    @Autowired
    private AuthService authService;
    @Autowired
    private ArticleImageService articleImageService;

    @ApiOperation("判断是否登录")
    @PostMapping("/canCreate")
    public Result canCreate(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        boolean login = authService.isLogin(token);
        if (!login){
            return AuthEnums.NO_LOGIN.getResult();
        }
        return PublicEnums.OK.getResult();
    }

    /**
     * 生成博客id
     * @return
     */
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "编号已生成", response = Result.class),
    })
    @ApiOperation("生成id编号")
    @PostMapping("/createId")
    public Result createId(){
        return ArticleEnums.IDCREATE.getResult().setData(IDUtil.createByUUID());
    }

    /**
     * 添加博客文章
     * @param addArticle
     * @return
     */
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "添加成功", response = Result.class),
            @ApiResponse(code = 400, message = "添加失败", response = Result.class),
            @ApiResponse(code = 401, message = "id为空", response = Result.class),
    })
    @ApiOperation("添加文章")
    @PostMapping("/addArticle")
    public Result addArticle(@RequestBody AddArticle addArticle){
        return articleService.addArticle(addArticle);
    }

    /**
     * 获取文章列表
     * @return
     */
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = Result.class),
    })
    @ApiOperation("获取文章列表")
    @GetMapping("/getAllArticle")
    @ResponseBody
    public Result getAllArticle(){
        return articleService.getAllArticle();
    }

    /**
     * 按时间分页查询文章
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = Result.class),
    })
    @ApiOperation("文件列表分页查询")
    @ResponseBody
    @GetMapping("/articleByTimePage/{currentPage}/{pageSize}")
    public Result articleByTimePage(@PathVariable(name = "currentPage") int currentPage,
                                        @PathVariable(name = "pageSize") int pageSize){
        return articleService.ArticleByTimePage(currentPage, pageSize);
    }

    /**
     * 按时间分页查询文章(Redis缓存)
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = Result.class),
    })
    @ApiOperation("文件列表分页查询(Redis缓存)")
    @ResponseBody
    @GetMapping("/articleByTimePageRedis/{currentPage}/{pageSize}")
    public Result articleByTimePageRedis(@PathVariable(name = "currentPage") int currentPage,
                                    @PathVariable(name = "pageSize") int pageSize){
        return articleService.articleByTimePageRedis(currentPage, pageSize);
    }


    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = Result.class),
    })
    @ApiOperation("获取指定文章")
    @ResponseBody
    @GetMapping("/showOneArticle")
    public Result showOneArticle(String id){
        return articleService.showOneArticle(id);
    }

    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = Result.class),
    })
    @ApiOperation("添加图片")
    @PostMapping("/uploadImage")
    public Result upload(MultipartFile file, ArticleImage articleImage)throws Exception {
        return articleImageService.addArticleImage(file, articleImage,articleImages);
    }

    @ApiOperation("通过标签查询文章")
    @PostMapping("/findByArticleType")
    public Result findArticleByType(@RequestBody FindByATypeDto findByATypeDto){
        return articleService.findArticleByType(findByATypeDto);
    }

}
